local CommentsController = {}
local vaild = require 'library.vaild'
local db = require 'models.db'
local helper = require 'library.helpers'
local page = require 'library.pageobj'

function CommentsController:index()
    local p = {}
    local page_params = {page=":page:"}
    local get = self:getRequest():getParams()
   
    local comments=db:new('comments') 
    local pageobj = page:new(comments:count(),12,tonumber(get['page']),helper:url('admin/comments/index',page_params));
    local res = comments:order('id desc'):limit(pageobj:limit()):get_list()

    p['page'] = pageobj;
    p['comments'] = res;

   return self:display(p)
end


function CommentsController:destroy()
    local p = {}
    local post=self:getRequest():POST()

    local id=helper:intval(post['id']) 

    if id>0 then
        local comments=db:new('comments') 
        comments:where('id=?',id):delete()
        self:redirect(helper:url('admin/comments/index'))
    end
    return helper:show_msg(1,'错误,没有找到记录',{url=helper:url('admin/comments/index')});
end

return CommentsController
